package com.oneplatform.cms.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.security.client.LoginContext;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.annotation.ApiPermOptions;
import com.oneplatform.base.constants.PermissionType;
import com.oneplatform.base.model.IdParam;
import com.oneplatform.base.model.PageResult;
import com.oneplatform.cms.dao.entity.ArticleEntity;
import com.oneplatform.cms.dto.param.ArticleParam;
import com.oneplatform.cms.dto.param.ArticleQueryParam;
import com.oneplatform.cms.service.ArticleService;

import io.swagger.annotations.ApiOperation;

/**
 * generated by www.jeesuite.com
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    private @Autowired ArticleService cmsArticleService;
	
	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ApiPermOptions(perms = PermissionType.Anonymous)
    public @ResponseBody PageResult<ArticleEntity> pageQueryCmsArticles(@RequestBody ArticleQueryParam param) {
		Page<ArticleEntity> page = cmsArticleService.pageQuery(param,param);
		return new PageResult<>(param.getPageNo(), param.getPageSize(), page.getTotal(), page.getData());
	}
	
	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ApiPermOptions(perms = PermissionType.Anonymous)
    public @ResponseBody WrapperResponse<ArticleEntity> getById(@PathVariable("id") int id) {
		ArticleEntity entity = cmsArticleService.findCmsArticleById(id);
		return new WrapperResponse<>(entity);
	}
	
	@ApiOperation(value = "新增文章")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ApiPermOptions(perms = PermissionType.Authorized)
    public @ResponseBody WrapperResponse<String> addCmsArticle(@RequestBody ArticleParam param) {
		ArticleEntity entity = BeanUtils.copy(param, ArticleEntity.class);
		entity.setCreatedAt(new Date());
		entity.setCreatedBy(LoginContext.getIntFormatUserId());
		cmsArticleService.addCmsArticle(entity);
		
		return new WrapperResponse<>();
	}
	
	@ApiOperation(value = "更新文章")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ApiPermOptions(perms = PermissionType.Authorized)
    public @ResponseBody WrapperResponse<String> updateCmsArticle(@RequestBody ArticleParam param) {
		ArticleEntity entity = BeanUtils.copy(param, ArticleEntity.class);
		entity.setUpdatedAt(new Date());
		entity.setUpdatedBy(LoginContext.getIntFormatUserId());
		cmsArticleService.updateCmsArticle(entity);
		
		return new WrapperResponse<>();
	}
	
	@ApiOperation(value = "删除文章")
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ApiPermOptions(perms = PermissionType.Authorized)
    public @ResponseBody WrapperResponse<String> deleteCmsArticle(@RequestBody IdParam param) {
		cmsArticleService.deleteCmsArticle(param.getId());
		return new WrapperResponse<>();
	}
	
	
}
